package coms.action;

import coms.delegates.ConferenceDelegate;
import coms.delegates.ConferenceRegistrationDelegate;
import coms.dto.Conference;
import coms.dto.ConferenceRegistration;
import coms.dto.User;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ConferenceBookingAction extends ComsActionSupport {

    private List<Conference> bookedConference;
    private List<ConferenceRegistration> registeredConferenceList;
    private String conferenceId;
    private List<Conference> conferenceList;
    private Map cancel = new HashMap();
    private String conferenceRegistration;
    private ConferenceRegistration confReg;
    private ConferenceRegistrationDelegate conferenceRegistrationDelegate;
    private ConferenceRegistration reg;
    private ConferenceDelegate conferenceDelegate;
    private boolean admin;

    public boolean isAdmin() {
        return admin;
    }

    public void setAdmin(boolean admin) {
        this.admin = admin;
    }

    public ConferenceBookingAction() {
        conferenceList = new ArrayList<Conference>();
        bookedConference = new ArrayList<Conference>();
        registeredConferenceList = new ArrayList<ConferenceRegistration>();
        conferenceRegistrationDelegate = new ConferenceRegistrationDelegate();
        reg = new ConferenceRegistration();
        conferenceDelegate = new ConferenceDelegate();
    }

    public ConferenceRegistration getConfReg() {
        return confReg;
    }

    public void setConfReg(ConferenceRegistration confReg) {
        this.confReg = confReg;
    }

    public String getConferenceRegistration() {
        return conferenceRegistration;
    }

    public void setConferenceRegistration(String conferenceRegistration) {
        this.conferenceRegistration = conferenceRegistration;
    }

    public List<ConferenceRegistration> getRegisteredConferenceList() {
        return registeredConferenceList;
    }

    public void setRegisteredConferenceList(List<ConferenceRegistration> registeredConferenceList) {
        this.registeredConferenceList = registeredConferenceList;
    }

    public Map getCancel() {
        return cancel;
    }

    public void setCancel(Map cancel) {
        this.cancel = cancel;
    }

    public List<Conference> getBookedConference() {
        return bookedConference;
    }

    public void setBookedConference(List<Conference> bookedConference) {
        this.bookedConference = bookedConference;
    }

    public String getConferenceId() {
        return conferenceId;
    }

    public void setConferenceId(String conferenceId) {
        this.conferenceId = conferenceId;
    }

    public void populateConference() {
        try {
            
            conferenceList = conferenceDelegate.findAll();
            
        } catch (Exception ex) {
            Logger.getLogger(ConferenceBookingAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String confirm() {
        try {

            Map<String, Object> user = getSession();
            User u = (User) user.get("user");
            if (u != null) {
                admin = u.isAdmin();
            }
            Conference con = new Conference();

            System.out.println(conferenceId);
            con = conferenceDelegate.getById(Integer.parseInt(conferenceId));

            ConferenceRegistration reg = new ConferenceRegistration(con);
            reg.setPaymentStatus(false);

            reg.setParticipant(u);
            reg.setValid(true);
            reg.setRegistrationDate(new Date());

            conferenceRegistrationDelegate.create(reg);

        } catch (Exception ex) {
            return "error";
        }
        return "bookingConfirmed";
    }

    public String cancel() {
        
        Map<String, Object> user = getSession();
        User u = (User) user.get("user");
        if (u != null) {
            admin = u.isAdmin();
        }
        for (Iterator i = cancel.keySet().iterator(); i.hasNext();) {
            String id = (String) i.next();
            int conferenceRegisterId = Integer.parseInt(id);
            ConferenceRegistration reg = null;
            try {
                reg = conferenceRegistrationDelegate.getById(conferenceRegisterId);
            } catch (Exception ex) {
                Logger.getLogger(ConferenceBookingAction.class.getName()).log(Level.SEVERE, null, ex);
            }
            reg.setValid(false);
            conferenceRegistrationDelegate.update(reg);
        }
        return view();
    }

    public String create() {
        populateConference();
        return "displayCreate";
    }

    public void populateBooked() {
        Map<String, Object> user = getSession();
            User u = (User) user.get("user");
            if(u!=null)
            {
                admin=u.isAdmin();
            }

        reg.setParticipant(u);

        registeredConferenceList = conferenceRegistrationDelegate.search(reg);        

    }

    public String payment() {
        System.out.println(conferenceRegistration);
        int confRegId = Integer.parseInt(conferenceRegistration);
        confReg = conferenceRegistrationDelegate.getById(confRegId);
        return "payment";
    }

    public String view() {
        populateBooked();
        return "displayBooked";
    }

    public List<Conference> getConferenceList() {
        return conferenceList;
    }

    public void setConferenceList(List<Conference> conferenceList) {
        this.conferenceList = conferenceList;
    }
}
